package com.youxuit.youxu.kecheng.SynchronizedDemo;

/**
 * 两个线程分别访问两个不同的同步方法演示
 *
 */
public class SynchronizedException9 implements Runnable {
    public static SynchronizedException9 instance = new SynchronizedException9();

    @Override
    public void run() {

        if ("t1".equals(Thread.currentThread().getName())) {
            methord1();
        } else {
            methord2();
        }
    }

    private synchronized void methord1() {
        System.out.println("methord1锁1我的线程名字是：" + Thread.currentThread().getName() + ",我开始运行了");

        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
        }
        System.out.println("methord1我的线程名字是：" + Thread.currentThread().getName() + "，运行结束了");
        throw new RuntimeException();
    }

    private synchronized void methord2() {
        System.out.println("methord2锁2我的线程名字是：" + Thread.currentThread().getName() + ",我开始运行了");

        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {

        }
        System.out.println("methord2我的线程名字是：" + Thread.currentThread().getName() + "，运行结束了");
    }

    public static void main(String[] args) {
        Thread t1 = new Thread(instance, "t1");
        Thread t2 = new Thread(instance, "t2");
        t1.start();
        t2.start();

        while (t1.isAlive() || t2.isAlive()) {

        }
        System.out.println("finish");
    }
}